using Microsoft.Phone.Controls.Properties;
using System;
using System.ComponentModel;
using System.Globalization;
namespace System.Windows.Controls
{
	public class TimeTypeConverter : TypeConverter
	{
		private static readonly string[] _timeFormats = new string[]
		{
			"h:mm tt",
			"h:mm:ss tt",
			"HH:mm",
			"HH:mm:ss",
			"H:mm",
			"H:mm:ss"
		};
		private static readonly string[] _dateFormats = new string[]
		{
			"M/d/yyyy"
		};
		public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType)
		{
			return Type.GetTypeCode(sourceType) == 18;
		}
		public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType)
		{
			return Type.GetTypeCode(destinationType) == 18 || TypeConverters.CanConvertTo<DateTime?>(destinationType);
		}
		public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
		{
			if (source == null)
			{
				return null;
			}
			string text = source as string;
			if (text == null)
			{
				string text2 = string.Format(CultureInfo.get_CurrentCulture(), Resources.TypeConverters_Convert_CannotConvert, new object[]
				{
					base.GetType().get_Name(),
					source,
					typeof(DateTime).get_Name()
				});
				throw new InvalidCastException(text2);
			}
			if (string.IsNullOrEmpty(text))
			{
				return null;
			}
			string[] timeFormats = TimeTypeConverter._timeFormats;
			DateTime dateTime;
			for (int i = 0; i < timeFormats.Length; i++)
			{
				string text3 = timeFormats[i];
				if (DateTime.TryParseExact(text, text3, CultureInfo.get_InvariantCulture(), 8, ref dateTime))
				{
					object result = DateTime.get_Now().get_Date().Add(dateTime.get_TimeOfDay());
					return result;
				}
			}
			string[] dateFormats = TimeTypeConverter._dateFormats;
			for (int j = 0; j < dateFormats.Length; j++)
			{
				string text4 = dateFormats[j];
				string[] timeFormats2 = TimeTypeConverter._timeFormats;
				for (int k = 0; k < timeFormats2.Length; k++)
				{
					string text5 = timeFormats2[k];
					if (DateTime.TryParseExact(text, string.Format(CultureInfo.get_InvariantCulture(), "{0} {1}", new object[]
					{
						text4,
						text5
					}), CultureInfo.get_InvariantCulture(), 0, ref dateTime))
					{
						object result = dateTime;
						return result;
					}
				}
			}
			string[] dateFormats2 = TimeTypeConverter._dateFormats;
			for (int l = 0; l < dateFormats2.Length; l++)
			{
				string text6 = dateFormats2[l];
				if (DateTime.TryParseExact(text, text6, CultureInfo.get_InvariantCulture(), 8, ref dateTime))
				{
					object result = dateTime;
					return result;
				}
			}
			string text7 = string.Format(CultureInfo.get_CurrentCulture(), Resources.TypeConverters_Convert_CannotConvert, new object[]
			{
				base.GetType().get_Name(),
				text,
				typeof(DateTime).get_Name()
			});
			throw new FormatException(text7);
		}
		public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
		{
			if (destinationType == typeof(string))
			{
				if (value == null)
				{
					return string.Empty;
				}
				if (value is DateTime)
				{
					return ((DateTime)value).ToString("HH:mm:ss", new CultureInfo("en-US"));
				}
			}
			return TypeConverters.ConvertTo(this, value, destinationType);
		}
	}
}
